package com.zyzd.general.module.recyclerview.layoutmanager;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.zyzd.commontools.utils.DeviceUtil;
import com.zyzd.general.R;
import com.zyzd.general.entity.local.UserEntity;
import com.zyzd.retrofit.base.BaseAdapter;

import java.util.Random;

/**
 * Created by lzy on 2018/5/18 0018.
 * description ${RecyclerView LayoutManager 案例 Adapter}
 */
class TestLayoutManagerAdapter extends BaseAdapter<RecyclerView.ViewHolder, UserEntity> {


    public static final int VIEW_TYPE_LINEAR = 0;
    public static final int VIEW_TYPE_GRID = 1;
    public static final int VIEW_TYPE_STAGGERED_GRID = 2;

    private int viewType = VIEW_TYPE_LINEAR;
    private Random mRandom = new Random();

    TestLayoutManagerAdapter(Context context) {
        super(context);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType) {
            case VIEW_TYPE_GRID:
                return new GridLayoutHolder(mInflater.inflate(R.layout.item_layoutmanager_grid, parent, false));
            case VIEW_TYPE_STAGGERED_GRID:
                return new StaggeredGridLayoutHolder(mInflater.inflate(R.layout.item_layoutmanager_staggered_grid, parent, false));
            case VIEW_TYPE_LINEAR:
            default:
                return new LinearLayoutHolder(mInflater.inflate(R.layout.item_layoutmanager_linear, parent, false));
        }
    }



    @Override
    public void onBindHolder(@NonNull final RecyclerView.ViewHolder holder, int position) {

        switch (getItemViewType(position)) {
            case VIEW_TYPE_GRID:
                ((GridLayoutHolder) holder).tvName.setText(getItem(position).getUserName());
                ViewGroup.LayoutParams layoutParams = ((GridLayoutHolder) holder).ivPortrait.getLayoutParams();
                int width = layoutParams.width;
                layoutParams.height = (int) (width * 0.75f);
                ((GridLayoutHolder) holder).ivPortrait.setLayoutParams(layoutParams);
                Glide.with(mContext).load(getItem(position).getHead_img_url())
                        .into(((GridLayoutHolder) holder).ivPortrait);
                break;
            case VIEW_TYPE_STAGGERED_GRID:

                ((StaggeredGridLayoutHolder) holder).tvName.setText(getItem(position).getUserName());
                ViewGroup.LayoutParams lp = ((StaggeredGridLayoutHolder) holder).ivPortrait.getLayoutParams();
                lp.width = DeviceUtil.getWidth() / 2;
                lp.height = (int) (lp.width * (0.5f + mRandom.nextFloat() * 1.5));
                Glide.with(mContext)
                        .load(getItem(position).getHead_img_url())
                        .asBitmap()
                        .override(lp.width, lp.height)
                        .into(new SimpleTarget<Bitmap>() {
                            @Override
                            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                                ((StaggeredGridLayoutHolder) holder).ivPortrait.setImageBitmap(resource);
                                Palette.from(resource).generate(new Palette.PaletteAsyncListener() {
                                    public void onGenerated(Palette p) {
                                        ((StaggeredGridLayoutHolder) holder).tvName.setBackgroundColor(p.getLightVibrantColor(ContextCompat.getColor(mContext, android.R.color.white)));
                                        ((StaggeredGridLayoutHolder) holder).tvName.setTextColor(p.getDarkMutedColor(ContextCompat.getColor(mContext, R.color.common_color_999)));
                                    }
                                });
                            }
                        });
                break;
            case VIEW_TYPE_LINEAR:
            default:
                ((LinearLayoutHolder) holder).tvName.setText(getItem(position).getUserName());
                Glide.with(mContext).load(getItem(position).getHead_img_url())
                        .into(((LinearLayoutHolder) holder).ivPortrait);
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return viewType;
    }

    static class LinearLayoutHolder extends RecyclerView.ViewHolder {

        private final TextView tvName;
        private final ImageView ivPortrait;

        public LinearLayoutHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            ivPortrait = itemView.findViewById(R.id.iv_portrait);
        }
    }

    static class GridLayoutHolder extends RecyclerView.ViewHolder {
        private final TextView tvName;
        private final ImageView ivPortrait;

        public GridLayoutHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            ivPortrait = itemView.findViewById(R.id.iv_portrait);
        }
    }

    static class StaggeredGridLayoutHolder extends RecyclerView.ViewHolder {
        private final TextView tvName;
        private final ImageView ivPortrait;

        public StaggeredGridLayoutHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
            ivPortrait = itemView.findViewById(R.id.iv_portrait);
        }
    }

    public int changeViewType() {
        this.viewType = (this.viewType + 1) % 3;
        return this.viewType;
    }

}
